package cqrtplm.controller;

import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.classification.model.dto.TyplmClassificationQueryTreeDTO;
import com.hustcad.plm.pdm.classification.model.vo.TyplmClassificationEntityTreeVO;
import com.hustcad.plm.pdm.classification.service.TyplmClassificationService;
import com.hustcad.plm.pdm.partbom.enums.PartBomErrorCodeEnum;
import com.ty.basic.constant.ApiPathConstant;
import com.ty.basic.entity.org.UserDO;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.session.TyAccountContext;
import com.ty.basic.utils.ArgumentUtils;
import cqrtplm.config.ConfigurableFieldsConfig;
import cqrtplm.entity.ProjectDevOrderDO;
import cqrtplm.service.DevOrderService;
import cqrtplm.service.RTOrgService;
import cqrtplm.service.impl.RtCfgStatusInfoServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;


/**
 * @Author：lotus
 * @DATE: 2025/7/7 11:07
 */

@RestController
@Api(tags = "项目开发令")
@RequestMapping(ApiPathConstant.PRODUCT_VERSION_ONE_PATH + "devorder")
public class RTProjectDevOrderController {

    @Resource
    private DevOrderService devOrderService;
    @Resource
    private TyplmClassificationService typlmClassificationService;
    @Resource
    private ConfigurableFieldsConfig configurableFieldsConfig;
    @Resource
    private RTOrgService rtOrgService;
    @Resource
    private RtCfgStatusInfoServiceImpl rtCfgStatusInfoService;


    @GetMapping("/getEnum")
    @ApiOperation(value = "获取枚举值", produces = "application/json; charset=utf-8")
    public ResponseResult<Map<String, Map<String, String>>> getProjectAttr() {
        Map<String, Map<String, String>> AttrMap= devOrderService.getAttr(configurableFieldsConfig.getFields());
        return ResponseResult.success(AttrMap);
    }


    @PostMapping({"/queryClassificationTree"})
    @ApiOperation(
            value = "根据对象类型或父分类oid查询分类树",
            notes = "根据对象类型或父分类oid查询分类树",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<TyplmClassificationEntityTreeVO>> queryClassificationTree() {

        TyplmClassificationQueryTreeDTO typlmClassificationQueryTreeDTO = new TyplmClassificationQueryTreeDTO();
        //项目开发令
        typlmClassificationQueryTreeDTO.setTypeOid(new BigInteger("321413117634428928"));
        typlmClassificationQueryTreeDTO.setOid(new BigInteger("707386470134841344"));
        return ResponseResult.success(this.typlmClassificationService.queryClassificationTree(typlmClassificationQueryTreeDTO));

    }

    @GetMapping("/queryProjectDevCodeByName")
    @ApiOperation(value = "根据型谱name查询项目开发令", produces = "application/json; charset=utf-8")
    public ResponseResult<List<ProjectDevOrderDO>> queryProjectDevCodeByName(String name) {
        List<ProjectDevOrderDO> projectDevOrderDOS = devOrderService.queryProjectDevCodeByName(name);
        return ResponseResult.success(projectDevOrderDOS);
    }

    @GetMapping("/queryByprojectCode")
    @ApiOperation(value = "根据项目开发令号查询", produces = "application/json; charset=utf-8")
    public ResponseResult<ProjectDevOrderDO> queryProjectDevOrderProjectCode(String projectCode) {
        if (StringUtils.isBlank(projectCode)) {
            return ResponseResult.fail("500","项目编号为空");
        }
        return ResponseResult.success(devOrderService.queryProjectDevOrderProjectCode(projectCode));
    }

    @GetMapping("/queryByNumber")
    @ApiOperation(value = "根据开发令查询", produces = "application/json; charset=utf-8")
    public ResponseResult<List<ProjectDevOrderDO>> queryProjectDevOrderByNumber(String number) {
        if (StringUtils.isBlank(number)) {
            return ResponseResult.fail("500","开发令为空");
        }
        return ResponseResult.success(devOrderService.queryProjectDevOrderByNumber(number));
    }

    @GetMapping("/queryByNumberAndName")
    @ApiOperation(value = "根据开发令和项目名称查询", produces = "application/json; charset=utf-8")
    public ResponseResult<List<ProjectDevOrderDO>> queryByNumberAndName(
            @RequestParam(required = false)String number,
            @RequestParam(required = false)String projectName,
            @RequestParam(required = false)String projectDevCode
            ) {
        List<ProjectDevOrderDO> projectDevOrderDOS =devOrderService.queryByNumberAndName(number,projectName,projectDevCode);
        return ResponseResult.success(projectDevOrderDOS);
    }



    @PostMapping("/insert")
    @ApiOperation(value = "插入", produces = "application/json; charset=utf-8")
    public ResponseResult insert(@RequestBody ProjectDevOrderDO devOrderDO) {
        devOrderService.insert(devOrderDO);
        return ResponseResult.success();
    }


    @GetMapping("/delete")
    @ApiOperation(value = "删除", produces = "application/json; charset=utf-8")
    public ResponseResult<?> deleteProjectDevOrderByProjectCode(String oid) {
        devOrderService.delete(oid);
        return ResponseResult.success();
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改开发令", produces = "application/json; charset=utf-8")
    public ResponseResult<?> updateProjectDevOrderByProjectCode(@RequestBody ProjectDevOrderDO devOrderDO) {
        devOrderService.update(devOrderDO);
        return ResponseResult.success();
    }

    @GetMapping("/queryAll")
    @ApiOperation(value = "获取所有开发令", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo<ProjectDevOrderDO>> queryAllDevOrder(
            @RequestParam(required = false) String projectCode,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "20") int pageSize) {

        return ResponseResult.success(devOrderService.queryAllDevOrder(projectCode,pageNum,pageSize));

    }


    @GetMapping("/getAllProductSpectrum")
    @ApiOperation(value = "获取产品型谱树结构", produces = "application/json; charset=utf-8")
    public ResponseResult getAllProductSpectrum() {
        return ResponseResult.success(devOrderService.getAllProductSpectrum());
    }

    @GetMapping("/queryProjectByNumber")
    @ApiOperation(value = "查询项目的开发令详情", produces = "application/json; charset=utf-8")
    public ResponseResult<ProjectDevOrderDO> queryProjectByNumber(BigInteger oid) {
        ProjectDevOrderDO projectDevOrderDO= devOrderService.queryProjectByNumber(oid);
        return ResponseResult.success(projectDevOrderDO);
    }

    @GetMapping("/checkUser")
    @ApiOperation(value = "校验用户", produces = "application/json; charset=utf-8")
    public ResponseResult<Boolean> checkUser() {
        UserDO user = TyAccountContext.getUser();
        Boolean result= rtOrgService.checkUser(user,"PMO");
        return ResponseResult.success(result);
    }

    @GetMapping({"/queryModelByDevCode"})
    @ApiOperation("根据开发令查询机型")
    public ResponseResult queryModelByDevCode(@Param("devCode") String devCode) {
        ArgumentUtils.checkArgumentNull(devCode, PartBomErrorCodeEnum.EXCEPTION_PARAM.getLocaleMessage(new Object[]{"devCode"}));
        String typeTrees = devOrderService.queryModelByDevCode(devCode);
        return ResponseResult.success(typeTrees);
    }

    @GetMapping({"/querySeriesByDevCode"})
    @ApiOperation("根据开发令＋机型查询产品系列")
    public ResponseResult querySeriesByDevCode(@Param("name") String name) {
        ArgumentUtils.checkArgumentNull(name, PartBomErrorCodeEnum.EXCEPTION_PARAM.getLocaleMessage(new Object[]{"name"}));
        String typeTrees = devOrderService.querySeriesByDevCode(name);
        return ResponseResult.success(typeTrees);
    }
    @GetMapping({"/generateMaturity"})
    @ApiOperation("获取对比结果")
    public ResponseResult generateMaturity(@Param("statusId") String statusId,@Param("productType") String productType) {
        List<RtCfgStatusInfoServiceImpl.MatchResult> matchResults = rtCfgStatusInfoService.generateMaturity(statusId,
                                                                                                            productType);
        System.out.println(matchResults);
        return ResponseResult.success(matchResults);
    }


}
